<?php
namespace Consumer\Controller;
use Common\Controller\AdminbaseController;
use Common\Model\MemberModel;

class MemberController extends AdminbaseController  {

    private $member_model;

    public function __construct()
    {
        parent::__construct();
        $this->member_model = new MemberModel();
    }

    public function lists(){
        $where = array();
        if(IS_POST) {
            $nickname = I('post.nickname');
            $username = I('post.username');
            $start_time = I('post.start_time');
            $end_time = I('post.end_time');
            if($nickname) {
                $where['nickname'] = array('like', '%'.$nickname.'%');
            }
            if($username) {
                $where['username'] = array('like', '%'.$username.'%');
            }
            if($start_time && $end_time) {
                $where['create_time'] = array('between', [$start_time.' 00:00:00', $end_time.' 23:59:59']);
            }
        } else {
            $nickname = I('get.nickname');
            $username = I('get.username');
            $start_time = I('get.start_time');
            $end_time = I('get.end_time');
            if($nickname) {
                $where['nickname'] = array('like', '%'.$nickname.'%');
            }
            if($username) {
                $where['username'] = array('like', '%'.$username.'%');
            }
            if($start_time && $end_time) {
                $where['create_time'] = array('between', [$start_time.' 00:00:00', $end_time.' 23:59:59']);
            }
        }
//dump($where);
        $count = $this->member_model
            ->where($where)
            ->count();
        $page = $this->page($count, C("PAGE_NUMBER"));
        $result = $this->member_model
            ->limit($page->firstRow . ',' . $page->listRows)
            ->where($where)
            ->select();

        $categorys = '';
        foreach ($result as $k => $v) {
            if($v['status'] == MemberModel::STATUS_SHOW) {
                $result[$k]['str_manage'] .= '<a href="' . U('Consumer/Member/detail', ['id' => $v['id']]) . '">查看信息</a> | <a href="' . U('Consumer/Member/freeze', ['id' => $v['id'], 'status' => '0']) . '" class="js-ajax-dialog-btn">冻结</a> | <a href="' . U('Consumer/Member/edit', ['id' => $v['id']]) . '">编辑</a> ';
            } else {
                $result[$k]['str_manage'] .= '<a href="' . U('Consumer/Member/detail', ['id' => $v['id']]) . '">查看信息</a> | <a href="' . U('Consumer/Member/freeze', ['id' => $v['id'], 'status' => '1']) . '" class="js-ajax-dialog-btn">解冻</a> | <a href="' . U('Consumer/Member/edit', ['id' => $v['id']]) . '">编辑</a>';
            }


            $categorys .= '<tr>
                <td>' . ($k + 1) . '</td>
                <td>' . $v['id'] . '</td>
                <td>' . $v['username'] . '</td>
                <td>' . $v['nickname'] . '</td>
                <td>' . $v['create_time'] . '</td>
                <td style="white-space:nowrap;">' . $result[$k]['str_manage']    . '</td>
            </tr>';

        }

        $this->assign('formget', I(''));
        $this->assign('tablebody', $categorys);
        $this->assign("Page", $page->show());
        $this->display();
    }

    public function detail() {
        $mid = I('get.id');
        $info = $this->member_model
            ->alias('a')
            ->where(['a.id' => $mid])
            ->field('a.*')
            ->find();

//        var_dump($info);
        $this->assign('button', $result['str_manage']);
        $info['header'] = setUrl($info['header']);
        $info['license_picture'] = setUrl($info['license_picture']);
        $info['id_card'] = setUrl($info['id_card']);
        $info['business_license'] = setUrl($info['business_license']);
        $this->assign('info', $info);
        $this->display();
    }


    /**
     * 冻结、解冻
     */
    public function freeze() {
        $mid = I('get.id');
        $status = I('get.status');

        $rst = $this->member_model->where(['id' => $mid])->setField('status', $status);
        if($rst) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 添加用户
     */
   public function add() {
       if(IS_POST) {
           $nickname = I('post.nickname');
           $password = I('post.password');
           if(strlen($password) < 6 || strlen($password) > 16) {
               $this->error('请输入6-16位密码');
           }
           if($this->member_model->where(['username' => $username])->count() > 0) {
               $this->error('该用户名已存在');
           }
            $header = upload_img('User');
           if(!$header) {
               $this->error('请选择头像');
           }
           $username = I('post.username');
           if(!preg_match("/^1[34578]{1}\d{9}$/",$username)){
               $this->error('请填写正确的手机号');
           }

           $data = [
               'username' => $username,
               'nickname' => $nickname,
               'password' =>sp_password($password),
               'header' => $header[0],
               'create_time' => date('Y-m-d H:i:s'),
           ];
            if($this->member_model->add($data)==false ) {
                $this->error('error');
            } else {
                $this->success();
            }
       } else {
           $this->display();
       }
   }

   public function edit() {
       $id = I('get.id');
       $info = $this->member_model->where(['id' => $id])->find();
       if(IS_POST) {
           $id = I('post.id');
//           dump($id);exit;
           $info = $this->member_model->where(['id' => $id])->find();//dump($info);exit;
           $nickname = I('post.nickname');
           $password = I('post.password');
           if(sp_password($password) != $info['password']) {
               if(strlen($password) < 6 || strlen($password) > 16) {
                   $this->error('请输入6-16位密码');
               }
           } else {
               $this->error('不能与原密码相同');
           }
            $data = [];
           $header = upload_img('User');
           if($header) {
               $data['header'] = $header[0];
           }
           if($password) {
               $data['password'] = sp_password($password);
           }

//dump($data);exit;
            if($data) {
                if($this->member_model->where(['id' => $id])->save($data) == false ) {
                    $this->error('error');
                } else {
                    $this->success();
                }
            } else {
               $this->error('没有修改项');
            }

       } else {
           $info['header'] = setUrl($info['header']);
           $this->assign('info', $info);
           $this->display();
       }
   }
}